package org.shilong.UI.party;

import java.util.LinkedList;

import org.shilong.MContext.Json.JSonContext;
import org.shilong.MContext.Party.PartyContext;
import org.shilong.Model.PartyModel;
import org.shilong.UI.R;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.ProgressDialogUtility;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 聚会的管理页面
 * @author Administrator
 *
 */
public class PartyCenterFrame extends Activity implements OnClickListener{

	private Button to_create_my_party_button, check_my_party_button;

	private  ListView listView;

	/*聚会的list*/
	public static LinkedList<PartyModel> pariesList = new LinkedList<PartyModel>();

	public final static String ACTION_NAME = "START_UPDATE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView( R.layout.layout_party_center_content );

		initViews();
		
		System.out.println("进入 Party Center");

		/*	MyTask task = new MyTask();

		task.execute();*/


		registerMyBroadcast();

		//handler.post( new MyJob() );

		//registerMyBroadcast();

	}

	private void registerMyBroadcast(){
		IntentFilter filter = new IntentFilter();
		
		filter.addAction(  ACTION_NAME );

		registerReceiver( new PartyStartTaskReceiver(), filter );
	}

	private void initViews(){

		listView = (ListView)this.findViewById( R.id.party_center_listview );

		to_create_my_party_button = (Button)this.findViewById( R.id. to_create_my_party_button ); 
		check_my_party_button = (Button)this.findViewById( R.id. check_my_party_button );

		to_create_my_party_button.setOnClickListener( this );
		check_my_party_button.setOnClickListener( this );

		listView.setAdapter( new MyAdapter( pariesList ));
	}




	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();


	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.to_create_my_party_button:

			toAddNewPartyLogic();

			break;

		case R.id.check_my_party_button:

			toCheckMyPartyLogic();

			break;

		default:
			break;
		}

	}


	private Handler handler = new Handler();

	private class MyJob implements Runnable{

		@Override
		public void run() {

			ProgressDialogUtility.showDialog( PartyCenterFrame.this, "读取中.." ); 

			String rlt = "nodatas";

			rlt = (String)PartyContext.getInstance( PartyContext.QUERY_ALL_PARTIES ).getRlt( null );

			System.out.println("获取的聚会信息是:" + rlt );

			LinkedList<PartyModel> ll = new LinkedList<PartyModel>();

			ll = (LinkedList<PartyModel>)JSonContext.createInstance( JSonContext.TYPE_GET_PARIES ).getJsonRlt( rlt );

			MyAdapter adp = new MyAdapter(ll);

			listView.setAdapter( adp );

			ProgressDialogUtility.cancelDialog();



		}


	}


	/**
	 * 查看我的 聚会页逻辑
	 */
	private void toCheckMyPartyLogic(){

		System.out.println("=========>查看我的聚会<============");
		IntentUtility.goNextFrame( PartyCenterFrame.this, PartiesOfMineFrame.class , null );

	}

	/**
	 * 创建我的 聚会页逻辑
	 */
	private void toAddNewPartyLogic(){

		System.out.println("=========>新建聚会<============");
		IntentUtility.goNextFrame( PartyCenterFrame.this, AddNewPartyFrame.class , null );

	}

	/*
	private class MyTask extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String rlt = "nodatas";

			rlt = (String)PartyContext.getInstance( PartyContext.QUERY_ALL_PARTIES ).getRlt( null );

			System.out.println("获取的聚会信息是:" + rlt );
			return rlt;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			ProgressDialogUtility.cancelDialog();

			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub


			super.onPreExecute();
		}




	}*/

	private class PartyStartTaskReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub

			handler.post( new MyJob() );


			System.out.println("启动程序更新程序");


		}

	}

	private class MyAdapter extends BaseAdapter{
		private LinkedList< PartyModel> mList;

		public MyAdapter( LinkedList< PartyModel> ll ){

			this.mList = ll;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub


			LayoutInflater inflater = (LayoutInflater)PartyCenterFrame.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

			View root = inflater.inflate( R.layout.inner_listview_party_layout, null );

			LinearLayout layout = (LinearLayout)root.findViewById( R.id.inner_party_box_layout );
			
			TextView title = (TextView)root.findViewById( R.id.party_listview_title_textview );
			TextView time = (TextView)root.findViewById( R.id.party_listview_time_textview );
			TextView content = (TextView)root.findViewById( R.id.party_listview_content_textview );

			if( arg0 % 2 != 0 ){
				layout.setBackgroundResource( R.drawable.msgbox_rec );
			}else{
				layout.setBackgroundResource( R.drawable.msgbox_send );
			}
			
			
			PartyModel model = new PartyModel();
			
			model = mList.get( arg0 );

			time.setText( "时间:" + model.getPa_start_time() + "--" + model.getEnd_time() );
			
			title.setText( "标题:" + model.getPa_title() );
			
			content.setText("描述:" + model.getPa_comment() );
			
			return root;
		}}

}
